<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *公告管理
*/
defined('BY_JYA') or exit('error');
$merchid=$this->checklogin();
$return['item']=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
$this->session('plugin_icon',$return['item']['logo']);
$return['plugin_icon']=$this->session('plugin_icon');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'员工列表'),
	array('op'=>'edit','title'=>'添加员工'),	
);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_merch_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['url']=custom_url('jy_merch/showdata/',true);
			//营销推广身份查询
			if($this->plugin_exists('jy_mteamfee')){
			    $team = pdo_get('jy_mteamfee_team',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			    $level = pdo_get('jy_mteamfee_level',array('id'=>$team['level_id'],'uniacid'=>$_SESSION['uniacid']));
			    
			    $item['yxlevel'] = $level['title'];
			}else{
			    $item['yxlevel'] = '无';
			}
			
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[<red>温馨提示</red>]:
		</br>1、绑定了粉丝的员工，可以通过手机端管理商品，订单等;
		</br>2、提示没有核销权限？说明你不是员工（注意：绑定员工会员的时候，小程序，微信公众号会员的差别！）
		</br>3、[普通店员],[管理员]：必须绑定准确的会员，员工前端才能免登录！
		','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'jobnum','title'=>'工号','type'=>'string'),
			array('name'=>'yxlevel','title'=>'营销推广身份','type'=>'string'),
			array('name'=>'type','title'=>'权限','type'=>'radio','options'=>'types'),
			array('name'=>'url','title'=>'数据查看','type'=>'copy'),			
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'iswq','title'=>'是否店外?','type'=>'switch','set'=>'dnset'),
		),'action'=>array(
			//array('action'=>'/manage.lesson_pai/','title'=>'排班','type'=>'href'),
		)),
		
		'search_one'=>array('name'=>'name'),
	);
	$return['htmls']=$htmls;
	$return['types']=array('核销员','普通店员','管理员');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}	
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['dnstatus']=array('0'=>'店内','1'=>'店外');
	$item['pass']='';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'姓名','type'=>'string'),
					array('name'=>'jobnum','title'=>'工号','type'=>'string'),
					array('name'=>'pass','title'=>'登录密码','type'=>'string'),
					array('name'=>'mobile','title'=>'电话','type'=>'string'),
					array('name'=>'sql_perms','title'=>'操作权限','type'=>'checkbox','options'=>'sql_perms'),
					array('name'=>'type','title'=>'权限','type'=>'radio','options'=>'types'),
					array('type'=>'choose_title','title'=>'选择会员','name'=>'mid','action'=>'jy_merch/get_member/','choose_title'=>'选择会员'),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
					array('name'=>'iswq','title'=>'是否店内?','type'=>'radio','options'=>'dnstatus'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	$return['types']=array('核销员','普通店员','管理员');	
	$return['sql_perms']=array('添加权限','编辑权限','删除权限','充值权限');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['mid']){
		exi('请绑定会员！','error');
	}
	if(!$postdata['jobnum']){
		exi('请填工号！','error');
	}
	$data=array_fields(array('name','mobile','title','mid','status','type','jobnum','pass','sql_perms','iswq'),$postdata);
	$data['title'] = $postdata['name'];
	if(!$postdata['pass']){
		unset($data['pass']);
	}else{
	    $data['pass']=base64_encode($postdata['pass']);
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_merch_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		pdo_insert('jy_merch_worker',$data);
		//添加操作系统插件人员

	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_merch_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的员工！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_merch_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}

if($_RQ['op']=='dnset'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的员工！','error');
	}
	$post=get_postdata();
	if(isset($post['iswq'])){
		pdo_update('jy_merch_worker',array('iswq'=>intval($post['iswq'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
